def combinations(items, n):
	"""
	Yields unique combinations of items taken n
	at a time.
	"""
	if n==0:
		yield []
	else:
		for i in xrange(len(items)):
			for cc in combinations(items[i+1:],n-1):
				yield [items[i]]+cc

def permutations(p):
    """Yields each permutation of the list p."""
    if len(p) <= 1:
        yield p
    else:
        for i in xrange(0, len(p)):
            p[0], p[i] = p[i], p[0]
            for q in permutations(p[1:]):
                yield [p[0]] + q
            p[i], p[0] = p[0], p[i]

